﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace FilStartUps.Helper
{
    public class Helper
    {
        //Generate Activation Key
        public static string CreateActKey()
        {
            //set string to empty
            string key = string.Empty;
            //generate new key with 20 string
            key = Guid.NewGuid().ToString().Substring(0, 20);
            return key;
        }

        // Calculate Age
        public static int CalculateAge(DateTime birthDate)
        {
            DateTime now = DateTime.Today;
            int years = now.Year - birthDate.Year;

            if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
                --years;

            return years;
        }

        //Truncate String
        public static string truncate(string description, int lenght)
        {
            if (description.Length < lenght)
            {
                return description;
            }
            else
            {
                return description.Substring(0, lenght) + "...";
            }
        }

        //Encode string 
        public static string base64Encode(string password)
        {

            try
            {
                //Instatiate ==> password.lenght
                byte[] encdata_byte = new byte[password.Length];
                //convert password to byte[]
                encdata_byte = System.Text.Encoding.UTF8.GetBytes(password);
                //Convert to base64string
                string encodedData = Convert.ToBase64String(encdata_byte);
                //return string;
                return encodedData;
            }
            catch (Exception ex)
            {
                throw new Exception("Error in base64Encode" + ex.Message);
            }
        }

        //Decode Password
        public static string base64Decode(string password)
        {
            try
            {
                //Instantiate UTF8Encoding
                System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
                System.Text.Decoder utf8Decode = encoder.GetDecoder();
                //Convert base64string to byte;
                byte[] todecode_byte = Convert.FromBase64String(password);
                int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char = new char[charCount];
                //Image - Decode ->Convert Data
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);
                //return string;
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception("Error in base64Decode" + ex.Message);
            }
        }

        public static string HashPassword(string password)
        {

            SHA1 hash = SHA1.Create();
            ASCIIEncoding encoder = new ASCIIEncoding();
            byte[] combined = encoder.GetBytes(password);
            hash.ComputeHash(combined);
            return Convert.ToBase64String(hash.Hash);

        }

    }
}